package cn.com.homeyoda.aimate.aihealth.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stx.xhb.xbanner.XBanner;
import com.stx.xhb.xbanner.transformers.Transformer;
import com.wzq.mvvmsmart.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.BR;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseFragment;
import cn.com.homeyoda.aimate.aihealth.bean.ArticleBean;
import cn.com.homeyoda.aimate.aihealth.bean.BannerBean;
import cn.com.homeyoda.aimate.aihealth.bean.KePuBean;
import cn.com.homeyoda.aimate.aihealth.bean.ReMenBean;
import cn.com.homeyoda.aimate.aihealth.bean.ZuiXinBean;
import cn.com.homeyoda.aimate.aihealth.databinding.FragmentHomeBinding;
import cn.com.homeyoda.aimate.aihealth.ui.activity.ActivityDetailActivity;
import cn.com.homeyoda.aimate.aihealth.ui.activity.ActivityListActivity;
import cn.com.homeyoda.aimate.aihealth.ui.game.GameActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health.ChronicDiseaseActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health.FoodActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health.SportActivity;
import cn.com.homeyoda.aimate.aihealth.ui.kepu.KePuActivity;
import cn.com.homeyoda.aimate.aihealth.ui.message.MessageActivity;
import cn.com.homeyoda.aimate.aihealth.ui.my.MyActivity;
import cn.com.homeyoda.aimate.aihealth.ui.my.family.manage.ManageActivity;
import cn.com.homeyoda.aimate.aihealth.ui.problem.ProblemActivity;
import cn.com.homeyoda.aimate.aihealth.ui.videoinspect.VideoInspectActivity;
import cn.com.homeyoda.aimate.aihealth.utils.ShowLoadingDialog;

/**
 * Created By 刘纯贵
 * Created Time 2020/4/30
 */
public class HomeFragment extends BaseFragment<FragmentHomeBinding, HomeViewModel> {

    private HomeAdapter mAdapter;
    private ZuiXinAdapter zuiXinAdapter;
    private ReMenAdapter reMenAdapter;

    private KePuAdapter kePuAdapter;
    private List<ArticleBean> beans = new ArrayList<>();

    private List<ZuiXinBean> zuiXinBeanList = new ArrayList<>();
    private List<ReMenBean> reMenBeanList = new ArrayList<>();
    private List<KePuBean> kePuBeanList = new ArrayList<>();
    private List<BannerBean> bannerBeans;
    private List<Integer> urls = new ArrayList<>();
    ;
    private ArticleBean articleBean = new ArticleBean();
    private ZuiXinBean zuiXinBean = new ZuiXinBean();
    private ReMenBean reMenBean = new ReMenBean();
    private KePuBean kePuBean = new KePuBean();
    private ShowLoadingDialog showLoadingDialog;

    @Override
    public HomeViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(HomeViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
//        showLoadingDialog = new ShowLoadingDialog(getContext());
//        showLoadingDialog.Show();
        //viewModel.requestData();
        viewModel.requestBannerData();
        viewModel.zuixinData();
        viewModel.remenData();
        viewModel.kepuData();
        initRecyclerView();
    }


    private void initRecyclerView() {
//
//        mAdapter = new HomeAdapter(R.layout.home_item,beans);
//        binding.setLayoutManager(new LinearLayoutManager(getActivity()));
//        binding.setAdapter(mAdapter);

        zuiXinAdapter = new ZuiXinAdapter(R.layout.zuixin_item, zuiXinBeanList);
        binding.setLayoutManager2(new LinearLayoutManager(getActivity()));
        binding.setAdapter2(zuiXinAdapter);

        reMenAdapter = new ReMenAdapter(R.layout.remen_item, reMenBeanList);
        binding.setLayoutManager3(new LinearLayoutManager(getActivity()));
        binding.setAdapter3(reMenAdapter);

        kePuAdapter = new KePuAdapter(R.layout.kepu_item, kePuBeanList);
        binding.setLayoutManager4(new GridLayoutManager(getActivity(), 2));
        binding.setAdapter4(kePuAdapter);


        zuiXinAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                zuiXinBean = zuiXinBeanList.get(position);
                Log.i("haha", zuiXinBean.getContent());
                startActivity(new Intent(getContext(), MessageActivity.class));
            }
        });

        zuiXinAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                zuiXinBean = zuiXinBeanList.get(position);
                Log.i("haha", zuiXinBean.getTitle());

            }
        });

        reMenAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                reMenBean = reMenBeanList.get(position);
                startActivity(new Intent(getActivity(), ActivityDetailActivity.class));
                Log.i("haha", reMenBean.getContent());
            }
        });

        kePuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                kePuBean = kePuBeanList.get(position);

                Log.i("haha", kePuBean.getContent());
            }
        });

        //监听下拉刷新完成
        viewModel.uc.finishRefreshing.observe(HomeFragment.this, new Observer<Object>() {
            @Override
            public void onChanged(@Nullable Object o) {
                binding.refreshLayout.finishRefresh();    //结束刷新
            }
        });
//        //监听上拉加载完成
//        viewModel.uc.finishLoadMore.observe(HomeFragment.this, new Observer<Object>() {
//            @Override
//            public void onChanged(@Nullable Object o) {
//                binding.refreshLayout.finishLoadMore();   //结束刷新
//            }
//        });

        binding.banner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object model, View view, int position) {
                BannerBean bannerBean = bannerBeans.get(position);
//                Intent intent = new Intent(getContext(), WebViewActivity.class);
//                intent.putExtra("link",bannerBean.getUrl());
//                intent.putExtra("title",bannerBean.getTitle());
//                startActivity(intent);
            }
        });

//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                articleBean = beans.get(position);
//                Intent intent = new Intent(getContext(), WebViewActivity.class);
//                intent.putExtra("link",articleBean.getLink());
//                intent.putExtra("title",articleBean.getTitle());
//                startActivity(intent);
//            }
//        });
//
//        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
//            @Override
//            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                articleBean = beans.get(position);
//                if (!articleBean.isCollect()){
//                    viewModel.collectionArticle(articleBean.getId());
//                }else {
//                    viewModel.unCollectionArticle(articleBean.getId());
//                }
//
//                viewModel.stateLiveCollData.stateEnumMutableLiveData
//                        .observe(getActivity(), new Observer<StateLiveData.StateEnum>() {
//                            @Override
//                            public void onChanged(StateLiveData.StateEnum stateEnum) {
//                                if (stateEnum.equals(StateLiveData.StateEnum.Success)){
//                                    //等待收藏接口回调成功后再请求列表数据接口，更新界面
//                                    viewModel.requestData();
//                                }
//                            }
//                        });
//
//                viewModel.stateLiveUnCollData.stateEnumMutableLiveData
//                        .observe(getActivity(), new Observer<StateLiveData.StateEnum>() {
//                            @Override
//                            public void onChanged(StateLiveData.StateEnum stateEnum) {
//                                if (stateEnum.equals(StateLiveData.StateEnum.Success)){
//                                    viewModel.requestData();
//                                }
//                            }
//                        });
//
//                viewModel.stateLiveData.stateEnumMutableLiveData
//                        .observe(getActivity(), new Observer<StateLiveData.StateEnum>() {
//                            @Override
//                            public void onChanged(StateLiveData.StateEnum stateEnum) {
//                                if (stateEnum.equals(StateLiveData.StateEnum.Success)){
//                                    mAdapter.notifyItemChanged(position);
//                                }
//                            }
//                        });
//            }
//        });
//
//        viewModel.stateLiveData.stateEnumMutableLiveData
//                .observe(getActivity(), new Observer<StateLiveData.StateEnum>() {
//                    @Override
//                    public void onChanged(StateLiveData.StateEnum stateEnum) {
//                        if (stateEnum.equals(StateLiveData.StateEnum.Idle)){
//                            showLoadingDialog.dismiss();
//                        }
//                        if (stateEnum.equals(StateLiveData.StateEnum.Error)){
//                            showLoadingDialog.dismiss();
//                        }
//                    }
//                });

        binding.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showShort("点击add");
            }
        });

        binding.llMine.setOnClickListener(v -> startActivity(new Intent(getContext(), MyActivity.class)));
        binding.chakan.setOnClickListener(v -> startActivity(new Intent(getContext(), MessageActivity.class)));
        binding.fankui.setOnClickListener(v -> startActivity(new Intent(getContext(), ProblemActivity.class)));
        binding.tvKepuMore.setOnClickListener(v -> startActivity(new Intent(getContext(), KePuActivity.class)));
        binding.tvActivityMore.setOnClickListener(v -> startActivity(new Intent(getContext(), ActivityListActivity.class)));
        // binding.llZixun.setOnClickListener(v -> startActivity(new Intent(getContext(), ZiXunActivity.class)));
        binding.experienceNow.setOnClickListener(v -> startActivity(new Intent(getContext(), VideoInspectActivity.class)));
        binding.experienceNow2.setOnClickListener(v -> startActivity(new Intent(getContext(), VideoInspectActivity.class)));
        binding.llGame.setOnClickListener(v -> startActivity(new Intent(getContext(), GameActivity.class)));
        binding.tijianRecord.setOnClickListener(v -> startActivity(new Intent(getContext(), ManageActivity.class)));
        binding.tijianRecordNow.setOnClickListener(v -> startActivity(new Intent(getContext(), ManageActivity.class)));
        binding.food.setOnClickListener(v -> startActivity(new Intent(getContext(), FoodActivity.class)));
        binding.sport.setOnClickListener(v -> startActivity(new Intent(getContext(), SportActivity.class)));


        binding.manbing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public void initViewObservable() {
//        viewModel.stateLiveData.observe(this,articleBeans -> {
//            beans = articleBeans;
//            mAdapter.setNewData(beans);
//        });

        viewModel.zuixinData.observe(this, zuiXinBeans -> {
            zuiXinBeanList = zuiXinBeans;
            zuiXinAdapter.setNewData(zuiXinBeanList);
        });

        viewModel.reMenData.observe(this, reMenBeans -> {
            reMenBeanList = reMenBeans;
            reMenAdapter.setNewData(reMenBeanList);
        });

        viewModel.kePuData.observe(this, kePuBeans -> {
            kePuBeanList = kePuBeans;
            kePuAdapter.setNewData(kePuBeanList);
        });

        viewModel.stateBannerLiveData.observe(this, bannerBeans1 -> {
            bannerBeans = bannerBeans1;
            Log.i("haha", "bannerBeans1.size=====" + bannerBeans1.size());
            urls.clear();
            for (BannerBean bean : bannerBeans) {
                urls.add(bean.getId());
            }
            Log.i("haha", "urls.size=====" + urls.size());
            if (urls != null && urls.size() > 0) {
                binding.banner.setData(urls, null);
                binding.banner.loadImage(new XBanner.XBannerAdapter() {
                    @Override
                    public void loadBanner(XBanner banner, Object model, View view, int position) {
                        Glide.with(getActivity())
                                .load(model) // model 是图片 URL 或资源
                                .apply(RequestOptions.bitmapTransform(new RoundedCorners(20))) // 设置圆角
                                .into((ImageView) view);
//                        Picasso.with(getActivity()).load(urls.get(position)).into((ImageView) view);
                    }
                });
            }
            binding.banner.setPageTransformer(Transformer.Default);
        });

//        binding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
//            @Override
//            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//                viewModel.pageNum++;
//                viewModel.requestData();
//            }
//
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                viewModel.pageNum = 0;
//                viewModel.requestData();
////                viewModel.requestBannerData();
//            }
//        });

    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_home;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
}
